I get this error although the code works:
2015/02/20 21:49:21 [error] 3730#0: 35 FastCGI sent in stderr: "PHP message: PHP Warning: preg_match(): Compilation failed: unmatched parentheses at offset 9 in /home/stefan/websites/Bootstrap/bootstrap.php on line 130" while reading response header from upstream, client: 127.0.0.1, server: bootstrap.dev, request: "GET /sr/proba HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "bootstrap.dev"
This is the code, but let me first explain what I was trying to achieve:
- I wanted to have translated routes and separate 404s for each module, so I have to find out language and module before defining router.
Line 130 would be $determinator->handle();
/**
* Registering language and module determinator
*/
private function registerLanguageAndModuleDeterminator()
{
$config = $this->di->get('config');
# Getting enabled languages
$enabledLanguages = array();
foreach ($config->languages as $language => $data)
{
if ($data['enabled'])
{
$enabledLanguages[] = $language;
}
}
# Ghost router used to match modules and langs
$determinator = new PhRouter(false);
$determinator->setDefaults(array(
'controller' => 'ghost',
'action' => 'ghost'
));
# Is it frontend?
$determinator->add('/:params)', array())->setName('frontend');
# Is it backend?
$determinator->add('/backend/:params', array())->setName('backend');
$determinator->handle();
$module = ($determinator->wasMatched()) ? $determinator->getRouteByName() : 'frontend';
$this->di->set('isBackend', function() use ($module) {
return ($module == 'backend');
}, true);
$this->di->set('module', function() use ($module) {
return $module;
}, true);
$params = $determinator->getParams();
$language = (isset($params[0]) && in_array($params[0], $enabledLanguages)) ? $params[0] : false;
if ($language)
{
$this->di->set('lang', function() use ($language) {
return $language;
}, true);
}
else
{
$defaultLang = $config->application->defaultLang;
$langMap = $config->offsetGet('countryLang');
# Get country based on GEOIP lookup
$country = (isset($_SERVER['GEOIP_COUNTRY_CODE'])) ? strtolower($_SERVER['GEOIP_COUNTRY_CODE']) : false;
$language = ($country && $langMap->get($country) && in_array($langMap->get($country), $enabledLanguages)) ? $langMap->get($country) : $defaultLang ;
$this->di->set('lang', function() use ($language) {
return $language;
}, true);
}
}